<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Item extends CI_Controller {
//Global variable  
    public $outputData;		//Holds the output data for each view
	
	public function __construct()
	{
         parent::__construct();
	 $this->load->model('Item_model','itemEntry');
         
         //$this->load->helper('json'); 
	}
	
	public function index()
	{
	   $this->load->view('person', $this->outputData);
	}
	 
     
        public  function addItemAjax(){
           // $this->load->view('home', $this->outputData);
            if($this->input->is_ajax_request()){
                $session_id = $this->session->userdata('session_id');
                $user_id = $this->session->userdata('user_id');
                if ($this->session->userdata('logged_in')) {
                    $this->itemEntry->insert_Item_For_User($user_id);
                }else{
                    $this->itemEntry->insert_Item_For_Guest($session_id);
                }
                
                $this->output->set_content_type('application/json');
                $this->output->set_output(json_encode(array('ConfirmationCode' => 'OK')));
            }else{
                $this->output->set_content_type('application/json');
                $this->output->set_output(json_encode(array('ConfirmationCode' => 'NOT_AJAX')));
            }
        }
      public function addItem()
      {
               $session_id = $this->session->userdata('session_id');
               $user_id = $this->session->userdata('user_id');
             if($_FILES["userfile"]["name"])
             {
                $config['upload_path'] = FCPATH.'img/avata/';
        		$config['allowed_types'] = 'gif|jpg|png';
        		$config['max_size']	= '1000';
        		$config['max_width']  = '2000';
        		$config['max_height']  = '2000';
                
                $config['file_name']=md5("avata".$session_id.time());
        
        		$this->load->library('upload', $config);
        
        		if ( ! $this->upload->do_upload())
        		{
        			$error = array('error' => $this->upload->display_errors());
        			$this->load->view('error_page', $error);
        		}
                else{
                    $upload_data=$this->upload->data();
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = FCPATH.'img/avata/'.$upload_data["file_name"]; 
                    $config['maintain_ratio'] = FALSE;
                    $config['width'] = 80;
                    $config['height'] = 80;
                    
                    $this->load->library('image_lib', $config); 
                    $this->image_lib->resize();
                    if ($this->session->userdata('logged_in')) {
                    $this->itemEntry->insert_Item_For_User($user_id,$upload_data["file_name"]);
                   }else{
                    $this->itemEntry->insert_Item_For_Guest($session_id,$upload_data["file_name"]);
                  }
               }
              }
              else
              {
                if ($this->session->userdata('logged_in')) {
                    $this->itemEntry->insert_Item_For_User($user_id);
                   }else{
                    $this->itemEntry->insert_Item_For_Guest($session_id);
                  }
              } 
              redirect(base_url());
                
      }
         
        public  function deleteItem($id){
           // $this->load->view('home', $this->outputData);
             $this->itemEntry->delete_Item($id);
             $this->output->set_content_type('application/json');
             $this->output->set_output(json_encode(array('ConfirmationCode' => 'OK')));
        }
        
        public function prev_ItemEntries($category_id, $item_index){
        $session_id = $this->session->userdata('session_id');
        $user_id = $this->session->userdata('user_id');
        
        
        $this->outputData['item_index'] = $item_index  + 1;
        
        
        if ($this->session->userdata('logged_in')) {
            //User is logged in
            $this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$user_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$user_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$user_id,$item_index);
        } else {
            //User is not logged in
            $this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$session_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$session_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$session_id,$item_index);
        }
        
        $this->load->view('category_first_entries', $this->outputData);
        }
        
    public function next_ItemEntries($category_id, $item_index){
        $session_id = $this->session->userdata('session_id');
        $user_id = $this->session->userdata('user_id');
        
        
        $this->outputData['item_index'] = $item_index - 1;
        
        
        if ($this->session->userdata('logged_in')) {
            //User is logged in
            $this->outputData['category_first'] = $this->itemEntry->next_five_entries_by_categoryId_SessionId($category_id,$user_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$user_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$user_id,$item_index);
        } else {
            //User is not logged in
            $this->outputData['category_first'] = $this->itemEntry->next_five_entries_by_categoryId_SessionId($category_id,$session_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$session_id,$item_index);
            //$this->outputData['category_first'] = $this->itemEntry->prev_five_entries_by_categoryId_SessionId($category_id,$session_id,$item_index);
        }
        
        $this->load->view('category_first_entries', $this->outputData);
        
       //echo 'jdjdjdj';
        }

}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */